<?php

include_once 'Base.php';
include_once 'page.class.php';
include_once 'veritas.php';
include_once 'snmp.php';

include_once dirname(dirname(__FILE__))."/vendor/SEPM_APIS/sepm_api.php";

class methods{
	//sqlserver连接
	public $SqlLink;
	//页面post提交的倿
	public $post = array();
	//参数
	public $parameter = array();
	//加密
	public $keys;
	//分页参数
	public $pages;
	//sql执行方式
	public $SqlCarried;
	//模块对应表名(节点删除时关联删除对应表内节点数据)
	public $Module_Table = array();
	
		//点击节点展示
	public function OnclickLike(){
		
		//查询节点属性
		$selectSql = "select [id],[name],[type] from [".$this->parameter['nodeTable']."] as tree where id = ".$this->post['id'];
		$dataSql = $this->SSgetAll($selectSql,1);
		//检测节点是否存
		if(!isset($dataSql)){
			echo json_encode(2);
			exit;
		}else{
			if(!isset($dataSql[0]['name'])){
				echo json_encode(2);
				exit;
			}
		}
		echo "<br>";
		echo "<b>node:".$dataSql[0]['name']."</b>";
		echo "<input type='hidden' id='id' value='".$dataSql[0]['id']."'/>";
		echo "<br>";
		echo "类型:<input type='text' id='types' value='".$dataSql[0]['type']."'/>";
		echo " ";
		echo "<input type='button' id='type' onclick='types()' value='设置'/>";
		echo "<br>";
		echo "<div id='divcss4'>";
		//根据节点类型判断所展示形式
		if(!ctype_space($dataSql[0]['type']) && $dataSql[0]['type']){	/*检测类型是否为空*/
			if(is_numeric($dataSql[0]['type'])){
				//根据类型选择展示方式
				$this->view($dataSql);
			}else{
				echo "error:type is int";
			}
		}else{
			echo "type:null";
		} 
		echo "</div>";
	}
	
	//根据类型选择展示方式
	/*
	*$dataSql  包含节点所有属性
	*/
	public function view($dataSql){
		
		//根据类型选择展示方式
		switch($dataSql[0]['type']) {
			case '1':	
				$this->sqlInteraction($dataSql);
				break;
			case '2':
				$veritas = new veritas($this->post['id'],$this->parameter['server']);
				$veritas->show();
				break;
			case '3':
				$MySnmp = new MySnmp($this->post,$this->parameter['server'],$this->SqlLink);
				$MySnmp->show();
				break;
			case '4':
				//include_once
				include_once dirname(dirname(__FILE__))."/vendor/Script/DBInteractive.php";
				$NBU = new NBU();
				$link = $NBU->TapeBackups();
				var_dump($link);
				break;
			case '5':
				//include_once
				include_once dirname(dirname(__FILE__))."/vendor/NetBackup_strategy/DBInteractive.php";
				$NBU = new NBU();
				$link = $NBU->TapeBackups();
				var_dump($link);
				break;
			case '6':				
				$SEPM_API = new SEPM_API($this->post);
				$SEPM_API->show();
				break;
			default:
				echo "未识别的类型";
				exit;
		}
	}
	//调用其它类导向方法
	public function AppClass(){
		
		switch($this->post['type']) {
			case '1':
				$MySnmp = new MySnmp($this->post,$this->parameter['server'],$this->SqlLink);
				
				$action = $this->post['ClassAction'];
				
				$MySnmp->$action();
				break;
			case '2':
								
				$SEPM_API = new SEPM_API($this->post);
				
				$action = $this->post['ClassAction'];
				
				$SEPM_API->$action();
				break;
			default:
				echo "未识别的类型";
				exit;
		}
	}
	//保存sql并展礿
	public function saveSql(){	
		
		//检测sql是否为空and sql是否为设置
		if(!ctype_space($this->post['sql']) && $this->post['sql']){		
			//检测sql执行方式是否为空and sql是否为设置
			if(!ctype_space($this->post['SqlCarried']) && $this->post['SqlCarried']){	
			
				//节点重命名是需转码为数据库可识别编砿
				$this->post['sql'] = iconv("utf-8","GB18030",$this->post['sql']);								
				//得到数据展示所需sql
				$sql = $this->post['sql'];			
				//检测sql是否需要转码
				$sql = $this->is_utf8($sql);
				
				//检验输入sql是否能执行成功
				/*if(!sqlsrv_query($this->SqlLink,$this->post['sql'])){
					echo json_encode(1);
					exit;
				}*/	
								
				//检测是否为查询(true 则展示查询结果 false 则展示影响行数)	
				/*if($this->post['SqlCarried'] == '分页'){
					//检测排序是否为空and sql是否为设置
					if(!ctype_space($this->post['primaryKey']) && $this->post['primaryKey']){
						//得到总页数
						$total = $this->totalF($sql);
						//数据表格展示
						@$this->showTable($sql,$total,'',$this->post['primaryKey']);
					}else{
						echo "排序为空";
					}
				}else{
					$sql_result = $this->sqlserverQuery($this->SqlLink,$sql);
					//检测sql语句是否运行成功
					if($sql_result['is_succeed']){
						echo "影响行数:".$sql_result['rows_affected']."<br>";
						if($sql_result['is_null']){
							$this->showDataTable($sql_result['list']);
						}
					}else{
						echo 'sql执行失败';
					}
				}*/
				
				
				//更新sql前加密
				$upSql = $this->encryption($this->post['sql']);

				//保存或更新sql前检测sqlserver表是否存在关联节点信息(没有则创建)
				$IS_node = $this->SSgetAll("select [node_ID] from [sql_CarriedOut] where [node_ID]=".$this->post['id'],1);
				//检测sql是否运行成功
				if(!isset($IS_node)){
					echo "<div id='save'>	检测sqlserver表是否存在关联节点信息 失败</div>";	
					exit;	
				}
				//检测节点是否存在(true  则不存在  则创建)
				if(!$IS_node){
					//创建关联节点server连接
					$carSql = "insert into [sql_CarriedOut]([node_ID]) values(".$this->post['id'].")";
					$save = $this->SSgetAll($carSql,1);
					if(!isset($save)){
						echo "<div id='save'>	创建关联节点  sqlserver连接 失败</div>";
						exit;
					}
				}

				//中文插入数据库前需将utf8转为GB18030格式
				$this->post['SqlCarried'] = iconv("utf-8","GB18030",$this->post['SqlCarried']);

				//修改节点sql
				$upSql = "update [sql_CarriedOut] set sql_CarriedOut.sql='".$upSql."',sql_CarriedOut.primaryKey='".$this->post['primaryKey']."',sql_CarriedOut.SqlCarried='".$this->post['SqlCarried']."' where sql_CarriedOut.node_ID=".$this->post['id'];
				
				$querys = sqlsrv_query($this->SqlLink,$upSql);

				//检测sql是否有错诿
				if(!$querys){
					echo json_encode(2);
					exit;
				}else{
					echo "<div id='save'>	save OK</div>";
				}
			}else{
				echo '执行方式未设置';		
			}				
		}else{
			echo json_encode(3);
			exit;
		}
	}
	//检测sql字符串编码是否为utf-8
	public function is_utf8($sql){

		$is = preg_match('/^.*$/u', $sql) > 0;//preg_match('/^./u', $string)
		
		//如果sql为utf8编码格式则转码为GB18030格式(sqlserver可识别格式)
		if($is === false){
			return $sql;
		}else{
			//sql运行前需将utf8转为GB18030格式
			$sql = iconv("utf-8","GB18030",$sql);
			return $sql;
		}
		
	}
	//检测sql字符串编码是否为GB18030
	public function is_gb18030($sql){

		$is = preg_match('/^.*$/u', $sql) > 0;//preg_match('/^./u', $string)
		
		//如果sql为utf8编码格式则转码为GB18030格式(sqlserver可识别格式)
		if($is === false){
			//sql运行前需将utf8转为GB18030格式
			$sql = iconv("GB18030","utf-8",$sql);
			return $sql;
			
		}else{
			return $sql;
		}
		
	}
	//执行sql展示
	public function performSql(){
		//检测sql是否为空and sql是否为设置
		if(!ctype_space($this->post['sql']) && $this->post['sql']){
			//去除sql两端空格
			$this->post['sql'] = trim($this->post['sql']);
			//检测sql执行方式是否为空and sql是否为设置
			if(!ctype_space($this->post['SqlCarried']) && $this->post['SqlCarried']){	
				//得到数据展示所需sql
				$sql = $this->post['sql'];
				//var_dump($sql);
				$sql = $this->is_utf8($sql);
				//var_dump($sql);die;
				//检测是否为查询(true 则展示查询结果 false 则展示影响行数)	
				if($this->post['SqlCarried'] == '分页'){
					//检测排序是否为空and sql是否为设置
					if(!ctype_space($this->post['primaryKey']) && $this->post['primaryKey']){
						//得到总条数
						$total = $this->totalF($sql);
						//数据表格展示
						@$this->showTable($sql,$total,'',$this->post['primaryKey']);
					}else{
						echo "排序为空";
					}
				}else{
					$sql_result = $this->sqlserverQuery($this->SqlLink,$sql);
		
					//检测sql语句是否运行成功
					if($sql_result['is_succeed']){
						echo "影响行数:".$sql_result['rows_affected'];
						if($sql_result['is_null']){
							//var_dump($sql_result['list']);die;
							$this->showDataTable($sql_result['list']);
						}
					}else{
						echo 'sql执行失败<br/>'.$sql_result['message'];
					}
				}
			}else{
				echo '执行方式未设置';
			}
		}else{
			echo json_encode(3);
			exit;
		}
	}
	//获取sql执行所得数据总条数
	public function totalF($SSsql){
		//检测sql是否需要转码
		$SSsql = $this->is_utf8($SSsql);
		
		 $result = sqlsrv_query($this->SqlLink,$SSsql);
		/*  var_dump($SSsql);
		 var_dump($this->SqlLink);
		 var_dump($result);*/
		 
		//检测sql执行是否成功
		if(!$result){
			echo "获取数据总条数失败";
			exit;
		}
		$i = 0;
        while($row = sqlsrv_fetch_object($result))
        {
			$i++;		
        } 
        return $i;
	}
	//数据以表格形式展示(不分页)
	/*
	*$rowData 数据展示所需数据
	*/
	public function showDataTable($rowData){
		//$list['table'] = array();
		//数据总条数	
		echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;总条数:".count($rowData);
		//展示新增sql查询的结枿
		echo "<div id='sql_table'>";
		echo '<table class="zebra">';
		//$list['table'][] = '<table class="zebra">';
		$i = 0;
		foreach($rowData as $data){
			//$data = $this->objectToArray($rowData);
			
			foreach($data as $key=>$value){
				if(!is_array($value)){
					$data[$key] = @iconv("GB18030","utf-8",$value);
				}else{
					$data[$key] = $value;
				}
			}
			//var_dump($data);
			if($i == 0){
				echo "<tr>";
				//$list['table'][] = "<tr>";
				foreach($data as $key => $value){
					//转码
					$key = $this->is_gb18030($key);
					echo "<th>".$key."</th>";
					//$list['table'][] = "<th>".$key."</th>";
				}
				echo "</tr>";
				//$list['table'][] = "</tr>";
			}
			//var_dump($list);die;
			echo "<tr>";
			//$list['table'][] = "<tr>";
			foreach($data as $key => $value){
				//遇到数组则认为是日期字段，则取date（猜测不严谨
				if(is_array($value)){
					echo "<td>".$value['date']."</td>";
					//$list['table'][] = "<td>".$value['date']."</td>";
				}else{
					echo "<td>".$value."</td>";
					//$list['table'][] = "<td>".$value."</td>";
				}
			}
			echo "</tr>";			
			//$list['table'][] = "</tr>";			
			$i++;	
		}
		
		echo "</table>";
		//$list['table'][] = "</table>";
		
		/* $values = null;
		foreach($list['table'] as $value){
			$values = $values.$value;
		}
		echo $values; */
		
		//echo json_encode($list['table']);
		echo "</div>";
		
	}
	//数据以表格形式展示(分页)
	/*
	*$sql 数据展示所需sql
	*$total 总页数
	*$currentPage 当前页
	*$primaryKey 排序所需字段
	*/
	public function showTable($sql,$total,$currentPage,$primaryKey){
		
		$sql = $this->is_utf8($sql);
		//var_dump($sql);
		//检测当前页是否设置，未设置默认当前页为1
		if(!$currentPage){
			$currentPage = 1;
		}else{
			//检测页数为数字，非数字默认当前页为1
			if(!is_numeric($currentPage)){
				$currentPage = 1;
			}else{
				//检测页数为负数，负数则默认当前页为1
				if(($currentPage<1)){
					$currentPage = 1;
				}
			}
		}
		
		//获取最大页数
		$maxPage = intval(($total / $this->pages['listRows'])+1);

		//检测当前页是否超出最大页，超出则重新设定当前页为最大页
		if($currentPage > $maxPage){
			$currentPage = $maxPage;
		}

		//检测是否展示最后一页
		if($currentPage == $maxPage){
			$listRows = ($total % $this->pages['listRows']);
		}else{
			$listRows = $this->pages['listRows'];
		}
		///展现当前页sql(分页)
		$sqlF = "SELECT
			*
		FROM
			(".$sql.") w1
		WHERE
			w1.".$primaryKey." IN (
				SELECT
					TOP ".$listRows." w.".$primaryKey."
				FROM
					(
						SELECT
							TOP (".$this->pages['listRows']."*".$currentPage.") w2.".$primaryKey."
						FROM
							(".$sql.") w2
						ORDER BY
							w2.".$primaryKey." asc
					) w
				ORDER BY
					w.".$primaryKey." desc
			)
		ORDER BY
			w1.".$primaryKey." asc";
		
		
		//var_dump($sqlF);
		//检测sql是否需要转码
		$sqlF = $this->is_utf8($sqlF);

		//运行数据展示所需sql
		$querys = sqlsrv_query($this->SqlLink,$sqlF);	
	
		//检测sql是否有错诿
		if(!$querys){
			echo 'error:sql On failure';
			exit;
		}

		$list['table'] = array();
		//展示新增sql查询的结枿
		echo "<div id='sql_table'>";
		$list['table'][] = '<table class="zebra">';
		$i = 0;
		while($rowData = sqlsrv_fetch_object($querys)){
			$data = $this->objectToArray($rowData);
			foreach($data as $key=>$value){
				if(!is_array($value)){
					$data[$key] = iconv("GB18030","utf-8",$value);
				}else{
					$data[$key] = $value;
				}
			}
			
			if($i == 0){
				$list['table'][] = "<tr>";
				foreach($data as $key => $value){
					
					//转码
					//$key = iconv("GB18030","utf-8",$key);
					
					//检测sql是否需要转码
					$key = $this->is_gb18030($key);
					
					$list['table'][] = "<th>".$key."</th>";
				}
				$list['table'][] = "</tr>";
			}
			$list['table'][] = "<tr>";
			foreach($data as $key => $value){
				//遇到数组则认为是日期字段，则取date（猜测不严谨
				if(is_array($value)){
					$list['table'][] = "<td>".$value['date']."</td>";
				}else{
					$list['table'][] = "<td>".$value."</td>";
				}
			}
			$list['table'][] = "</tr>";			
				$i++;			
		}
		$list['table'][] = "</table>";
		
		$values = null;
		foreach($list['table'] as $value){
			$values = $values.$value;
		}
		echo $values;
		
		//echo json_encode($list['table']);
		echo "</div>";
		//sql加密
		$sqlE = $this->encryption($sql);
		
		//字符串处理（两端加上""）
		$sqlE = '"'.$sqlE.'"';
		
		//实例化分页类
		$page = new Page($total,$this->pages['listRows'],$currentPage);//参数:(总条数,每页展示条数,当前页数)
		$page->links=$sqlE;
		echo "<br/>";	
		echo $page->fpage($this->pages['show']);
		

		
	}
	//分页
	public function page(){

		//解密sql
		$sql = $this->decryption($this->post['links']);
		$this->showTable($sql,$this->post['number'],$this->post['pages'],$this->post['primaryKey']);
	}

	//类型设置
	public function type(){
		//var_dump($this->post);
		//根据节点类型判断所展示形式
		if(!ctype_space($this->post['types']) && $this->post['types']){	/*检测类型是否为空*/

			if(is_numeric($this->post['types'])){
				
				//更新类型字段
				$upTypeSql = "update [".$this->parameter['nodeTable']."] set type=".$this->post['types']." where id=".$this->post['id'];
				if($this->SSquery($upTypeSql,1)){
					echo "<div id='save'>	save OK</div>";
				}
				
				//查询节点属性
				$selectSql = "select * from [".$this->parameter['nodeTable']."] as tree where id = ".$this->post['id'];
				$dataSql = $this->SSgetAll($selectSql,1);
				//检测节点是否存
				if(!isset($dataSql)){
					echo json_encode(2);
					exit;
				}else{
					if(!isset($dataSql[0]['name'])){
						echo json_encode(2);
						exit;
					}
				}
				//根据类型选择展示方式
				$this->view($dataSql);
				
			}else{
				echo "error:type is int";
			}
		}else{
			echo "type:null";
		} 
	}
	
	//sql交互页面
	public function sqlInteraction($nodeData){
		//var_dump($nodeData);
		$sql = "select [sql],[primaryKey],[SqlCarried] from [sql_CarriedOut] where [node_ID] = ".$nodeData[0]['id'];
		
		$dataSql = $this->SSgetAll($sql,1);

		//检测节点是否存
		if(!isset($dataSql) || !$dataSql){
			$dataSql[0] = array(
				'sql'=>'',
				'SqlCarried'=>'',
				'primaryKey'=>'',
			);
		}
		//sql解密
		$dataSql[0]['sql'] = $this->decryption($dataSql[0]['sql']);
		
		//sql转码为utf8格式，用于展示(GB18030转为utf8)
		$dataSql[0]['sql'] = iconv("GB18030","utf-8",$dataSql[0]['sql']);
		echo "<br>";
		//sql展示
		echo "<textarea id='sql' style='width:1000px;height:80px;'>".$dataSql[0]['sql']."</textarea>";
		echo "<br><br>";
		//检测sql执行方式是否设置,如果已设置则  将设置值从默认值内删除
		echo "执行方式:<select onchange='SqlCarried()' id='SqlCarried'>";
		
		if($dataSql[0]['SqlCarried']){
			unset($this->SqlCarried[$dataSql[0]['SqlCarried']]);
			//sql转码为utf8格式，用于展示(GB18030转为utf8)
			//$dataSql[0]['SqlCarried'] = iconv("GB18030","utf-8",$dataSql[0]['SqlCarried']);
			echo "<option value='".$dataSql[0]['SqlCarried']."'>".$dataSql[0]['SqlCarried']."</option>";
		}else{
			echo "<option value='请选择执行方式'>请选择执行方式</option>";
		}
		foreach($this->SqlCarried as $key=>$value){
			echo "<option value='".$key."'>".$value."</option>";
		}
		echo"</select>";
		echo " ";
		echo "<die id='select_id'>";
		if($dataSql[0]['SqlCarried'] == '分页' || !$dataSql[0]['SqlCarried']){
			//排序所需字段
			echo "分页排序字段:<input type='text' id='primaryKey' value='".$dataSql[0]['primaryKey']."'/>";
		}
		echo "</die>";		
		echo "<br><br>";
		echo "<input type='button' id='btn' onclick='perform()' value='预览'/>";
		echo " ";
		echo "<input type='button' id='btn' onclick='save()' value='保存'/>";
		echo "<br><br>";
		echo "<div id='divcss5'>";
		//点击节点默认显示sql运行结果
		/*//检测sql运行方式是否为查询(true 则运行sql得到结果)
		if($dataSql[0]['SqlCarried'] == '分页'){
			//检测sql是否为空and sql是否为设置
			if(!ctype_space($dataSql[0]['sql']) && $dataSql[0]['sql']){
				//检测排序是否为空and sql是否为设置
				if(!ctype_space($dataSql[0]['primaryKey']) && $dataSql[0]['primaryKey']){
					//得到数据展示所需sql
					$sql = $dataSql[0]['sql'];
					
					//得到总页数
					$total = $this->totalF($sql);
					
					//数据表格展示
					@$this->showTable($sql,$total,'',$dataSql[0]['primaryKey']);
				}else{
					echo "排序为空";
				}
			}else{
				echo "sql null";
			} 
		}*/		
		echo "</div>";
		
	}	
	//修改节点pid（用于节点拖动）
	public function moveNode(){
		$sql = "update [".$this->parameter['nodeTable']."] set pid='".$this->post['targetId']."' where id=".$this->post['operateId'];
		//检测sql是否需要转码
		$sql = $this->is_utf8($sql);
		$query = sqlsrv_query($this->SqlLink,$sql);
		//检测节点删除是否成势
		if($query){
			$query = 1;
		}else{
			$query = 0;
		}
		echo json_encode($query);
	}
	//删除节点
	public function removeNode(){
		
		$node = json_decode($this->post['IdAll'],true);
		//检测是否为根节炿
		if($node['pId'] == null){
			echo json_encode(2);
			exit;
		}
		$array = $this->removeAll($node,'id');
		$where = implode(",",$array);
		
		//获取节点类型
		$node_type = $this->sqlserverQuery($this->SqlLink,"select [id],[type] from [".$this->parameter['nodeTable']."] where id in(".$where.")");
		
		if(!$node_type['is_succeed']){
			echo "获取节点类型 失败  ".$node_type['message'];
			exit;
		}
		
		//循环删除其他模块关联此节点信息
		foreach($node_type['list'] as $value){
			$Module_Table_f = $this->Module_Table[$value['type']];
			//检测类型是否设置
			if($value['type']){
				$del_node_module = $this->sqlserverQuery($this->SqlLink,"delete from [".$this->Module_Table[$value['type']]."] where [node_ID] = ".$value['id']);
				if(!$del_node_module['is_succeed']){
					echo "表:".$this->Module_Table[$value['type']]."  node_ID=".$value['id']."删除失败<br/>".$del_node_module['message'];
				}
			}
		}
		
		
		$sql = "delete from [".$this->parameter['nodeTable']."] where id in(".$where.")";
		
		$query = sqlsrv_query($this->SqlLink,$sql);
		//检测节点删除是否成势
		if($query){
			$query = 1;
		}else{
			$query = 0;
		}
		echo json_encode($query);
	}
	//修改节点
	public function editNode(){
		if(!$this->post['name']){
			echo json_encode(2);
			exit;
		}
		//节点重命名是需转码为数据库可识别编码
		$this->post['name'] = iconv("utf-8","GB18030",$this->post['name']);
		
		$sql = "update [".$this->parameter['nodeTable']."] set name='".$this->post['name']."' where id=".$this->post['id'];
		//检测sql是否需要转码
		$sql = $this->is_utf8($sql);
		$query = sqlsrv_query($this->SqlLink,$sql);
		//检测节点更新是否成势
		if($query){
			$query = 1;
		}else{
			$query = 0;
		}
		echo json_encode($query);
	}
	//添加节点,只能添加单节炿
	public function addNodes(){	
		//检测是否存在父节点

		$data= $this->SSgetAll("select id from [".$this->parameter['nodeTable']."] where id=".$this->post['pId'],1);
	
		if(!isset($data[0])){
			echo json_encode(0);
			exit;
		}
		//获取guid
		$nodeGuid = $this->create_guid();
		//检测新加节点名称是否为穿
		if(!isset($this->post['name'])){
			$error['error'] = 2;

			echo json_encode($error);
		}
		
		$sql = "insert into [".$this->parameter['nodeTable']."](pId,name,nodeGuid) values(".$this->post['pId'].",'".$this->post['name']."','".$nodeGuid."')";
		
		//{"error":1,"id":{"id":3662}}
		//{"error":0}

		$query = sqlsrv_query($this->SqlLink,$sql);
		//检测节点添加是否成势
		if($query){
			$error['error'] = 1;
			//获取添加节点的自增长id
			$sqlId = "select id from [".$this->parameter['nodeTable']."] where nodeGuid = '".$nodeGuid."'";
			
			$id = sqlsrv_query($this->SqlLink,$sqlId);
			
			while($row = sqlsrv_fetch_object($id)){
				$idData['id'] = $row;
			}			
			$addLike = array_merge($error,$idData);

			echo json_encode($addLike);
		}else{
			$error['error'] = 0;

			echo json_encode($error);
		}		
	}
	//节点展示
	public function init_data_list(){
		
		$sql = "SELECT id,pId,name,[open] FROM [".$this->parameter['nodeTable']."]";
		
		$data = $this->SSgetAll($sql,1);
		//$data = iconv("GB18030","utf-8",$data);
		//$data = iconv('UTF-8','GB2312',$data);
		//var_dump($data);
		echo json_encode($data);

	}
	//节点折叠
	public function OnCollapse(){
		
		$sql = "update [".$this->parameter['nodeTable']."] set [open]='false' where id=".$this->post['id'];
		$this->SSquery($sql,1);
		
	}
	
	//节点展开
	public function OnExpand(){
		
		$sql = "update [".$this->parameter['nodeTable']."] set [open]='true' where id=".$this->post['id'];
		$this->SSquery($sql,1);
		
	}
	//递归获取数组内指定倿
	public function removeAll($arr,$keyName){	
		foreach($arr as $key => $value){
			if($key === $keyName){
				$array[] = $value;
			}
			if(is_array($value)){
				$arrays[] = $this->removeAll($value,$keyName)[0];
				if(!isset($array)){
					$array = array();
				}
				$array = array_merge($this->removeAll($value,$keyName),$array);
			}
		}
		if(isset($array)){
			return $array;
		}else{
			return;
		}
		
	}
	//生成guid
	public function create_guid(){
		$charid = strtoupper(md5(uniqid(mt_rand(),true)));
		$hyphen = chr(45);
		$uuid = chr(123)
		.substr($charid,0,8).$hyphen
		.substr($charid,8,4).$hyphen
		.substr($charid,12,4).$hyphen
		.substr($charid,16,4).$hyphen
		.substr($charid,20,12)
		.chr(125);
		return $uuid;
	}


	
	
	
	
	
	/**********************sqlServer*************************************************/
	public function SSquery($sql,$error){
		
		//检测sql是否需要转码
		$sql = $this->is_utf8($sql);
        $result = sqlsrv_query($this->SqlLink,$sql);
		
		//检测sql执行是否成功
		if(!$result){
			echo json_encode($error);
			exit;
		}else{
			return true;
		}
    }

	//sql server输出查询结果
    public function SSgetAll($SSsql,$error){
		//检测sql是否需要转码
		$SSsql = $this->is_utf8($SSsql);
	
        $result = sqlsrv_query($this->SqlLink,$SSsql);
		//检测sql执行是否成功
		if(!$result){
			echo json_encode($error);
			exit;
			//return $error;
		}
		unset($ary);
		$ary = array();
		$i = 0;
        while($row = sqlsrv_fetch_object($result))
        {
			$i++;
			unset($rowss);
			unset($values);
			$rowss = array();
			$values = array();
			//var_dump($row);
			$rows = $this->objectToArray($row);
			///* if(!empty($MStable['field'])){
				foreach($rows as $key=>$value){
					$rows[$key] = iconv("GB18030","utf-8",$value);
				}
			//} */
            $ary[] = $rows;
        }
        return $ary;
    }
	/*sqlserver输出查询结果
	*$sqlserverCon			sqlserver数据库连接资源
	*$sql					要运行的sql
	*返回值解释：		
	$status = array(
		is_succeed			sql是否执行成功
		rows_affected		受影响行数
		is_null				执行结果是否为空
		list				执行结果
	)
	*/
	public function sqlserverQuery($sqlserverCon,$sql){

		$list = sqlsrv_query($sqlserverCon,$sql);
//		print_r(sqlsrv_errors());


		if(!$list){
			$status['is_succeed'] = '0';
            $status['message'] = '';
            foreach (sqlsrv_errors () as $error){

                $status['message'] .=$this->is_gb18030($error['message']);
                $status['message'] .="<br/>";
            }
			return $status;
		}else{
			$status['is_succeed'] = '1';
            $status['message'] = '';
			$status['rows_affected'] = sqlsrv_rows_affected($list);
		}
		$sqlList = array();
		while($res=sqlsrv_fetch_object($list)){
			$data[] = $this->objectToArray($res);
		}
		//检测查询结果是否为空
		if(!empty($data)){
			$status['list'] = $data;
			$status['is_null'] = '1';
		}else{
			$status['is_null'] = '0';
		}
		return $status;
	}
	//递归将sqlserver查询结果对象转化为数绿
	public function objectToArray($row){
		$arr = is_object($row) ? get_object_vars($row) : $row;		
		if(is_array($arr)){
			foreach($arr as $key=>$value){
				if(is_object($value)){
					$data[$key] = $this->objectToArray($value);
				}else{
					$data[$key] = $value;
				}
				
			}
			return $data;
		}else{
			return $arr;
		}
		return $arr;
	}
	//加密
	public function encryption($input){
		$base = new Base();
		$base->config = $this->keys;
		$input = $base->encrypt($input);
		return  $input;
	}
	//解密
	public function decryption($input){
		$base = new Base();
		$base->config = $this->keys;
		$input = $base->decrypt($input);
		return  $input;
	}
}

?>